package com.jui.material.views;

import com.jui.material.R;
import com.jui.material.utils.Utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ButtonFlat extends Button {
	
	TextView textView;
	int textColor;

	public ButtonFlat(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}
	
	protected void setDefaultProperties(){
		minHeight = 36;
		minWidth = 88;
		rippleSize = 3;
		// Min size
		setMinimumHeight(Utils.dpToPx(minHeight, getResources()));
		setMinimumWidth(Utils.dpToPx(minWidth, getResources()));
		setBackgroundResource(R.drawable.background_transparent);
	}

	@Override
	protected void setAttributes(AttributeSet attrs) {
		// Set text button
		String text = null;
		float size = -1;
		int textColorRes = attrs.getAttributeResourceValue(ANDROIDXML, "textColor", R.color.md_textColor);
		int textSizeRes = attrs.getAttributeResourceValue(ANDROIDXML, "textSize", R.dimen.md_textSize);
//      int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",-1);
//		if(textResource != -1){
//			text = getResources().getString(textResource);
//		}else{
//			text = attrs.getAttributeValue(ANDROIDXML,"text");
//		}
		int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",R.string.md_text);
        text = getResources().getString(textResource);
        size = getResources().getDimension(textSizeRes);
        textColor = getResources().getColor(textColorRes);
		if(text != null){
		  textView = new TextView(getContext());
		  textView.setText(text);
		  textView.setTextColor(textColor);
		  textView.setTextSize(size);
		  textView.setTypeface(null, Typeface.NORMAL);
		  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		  params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		  textView.setLayoutParams(params);
		  addView(textView);
		}
//		int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
//		if(bacgroundColor != -1){
//			setBackgroundColor(getResources().getColor(bacgroundColor));
//		}else{
//			background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
//			if (background != -1)
//				setBackgroundColor(background);
//		}
		int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",R.color.md_backgroudColor);
        setBackgroundColor(getResources().getColor(bacgroundColor));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (x != -1) {
			
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(makePressColor());
			canvas.drawCircle(x, y, radius, paint);
			if(radius > getHeight()/rippleSize)
				radius += rippleSpeed;
			if(radius >= getWidth()){
				x = -1;
				y = -1;
				radius = getHeight()/rippleSize;
				if(onClickListener != null&& clickAfterRipple)
					post(new Runnable() {
                      
                      @Override
                      public void run() {
                        // TODO Auto-generated method stub
                        onClickListener.onClick(ButtonFlat.this);
                      }
                    });
			}
			invalidate();
		}		
		
	}
	
	/**
	 * Make a dark color to ripple effect 设置脉冲颜色
	 * @return
	 */
	@Override
	protected int makePressColor(){
		return getResources().getColor(R.color.md_ripple_color);	
	}
	
	public TextView getTextView() {
		return textView;
	}
	
	public String getText(){
        	return textView.getText().toString();
 	}
	public void setText(String text) {
      textView.setText(text);
    }
	public void setTextColor(int color) {
      textView.setTextColor(color);
    }
	public void setTextSize(float size){
	  textView.setTextSize(size);
	}
	public void setTextStyle(int style){//style values:Typeface.NORMAL,Typeface.BOLD,Typeface.ITALIC,Typeface.BOLD_ITALIC
      textView.setTypeface(null, style);
    }

  @Override
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if (!enabled) {
      textView.setTextColor(getResources().getColor(R.color.md_invalid_textColor));
    }else {
      textView.setTextColor(textColor);
    }
  }
    

}
